(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	var currentPage = 1;
	var pageSize = 10;
	var totalSize;
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initTime();
			getGradeAttence();
		});
	});
	function initTime(){
		var date = new Date();
		var newDate = getFormatDate(date,"yyyy-MM-dd hh:mm");
		var startTime = getFormatDate(new Date(date.getTime()-1000*60*60*24),"yyyy-MM-dd ");
		$("#start-time").val(startTime+"00:00");
		$("#end-time").val(newDate);
//		$("#start-time").val(newDate + " 08:00");
//		$("#end-time").val(newDate + " 23:00");
//		$("#start-time").val(getFormatDateByLong(1425571200000,"yyyy-MM-dd hh:mm"));
//		$("#end-time").val(getFormatDateByLong(1457452800000,"yyyy-MM-dd hh:mm"));
	}
	function getGradeAttence(){
		processBegin("数据加载中······");
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var studentcode = $(".students-choice-select",parent.document).attr("studentcode");
		var starttime = strToDate($("#start-time").val() + ":00");
		var endtime = strToDate($("#end-time").val() + ":00");
		var status = $("#st-grade option:selected").val();
		var obj = {};
		obj.starttime = starttime+"";
		obj.endtime = endtime+"";
		obj.studentcode = studentcode;
		if(status == "5"){
			obj.remark = status;
		}else{
			obj.status = status;
		}
//		var obj = {
//				starttime:starttime+"",
//				endtime:endtime+"",
//				studentcode:studentcode,
//				status:status,
////				currentpage:1,
////				pagesize:4
//		};
		var data = {
				cmd:"queryIntoSchoolRecord",
				model:"attendance",
				userKey:userKey,
				data:obj,
				remark:"",
				
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var list = res.data.list;
				if(list.length > 0){
					$("#inout-data-show").empty();
					$("#inout-data-show").html(_.template($("#data-template").html(),list));
					processEnd("加载成功");
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#inout-data-show").empty();
					$("#inout-data-show").append(content);
					processEnd("加载成功");
				}
			}else{
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#inout-data-show").empty();
				$("#inout-data-show").append(content);
				processError("加载失败");
			}
		},null,null);	
	}
	function substr(str,len){
		if(str != undefined){
			if(str.length > len){
				str = str.substring(0,len-2)+"...";
				return str;
			}else{
				return str;
			}
		}else{
			return "";
		}
	}
	function leadingOutAttences(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var starttime = strToDate($("#start-time").val() + ":00")/1000;
		var endtime = strToDate($("#end-time").val() + ":00")/1000;
		var data = {
			cmd: "getAttendanceByClasscodeByPage",
			model: "attendance",
			userKey: userKey,
			data:{
				starttime: starttime,
				endtime: endtime,
				isdownload: "true",
			},
			remark:"",						
		};
		processBegin("正在导出中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				if(res.remark.flag == true){
					var url = Host + "/downloads/" + res.remark.msg;
					window.frames["hrong"].location = url;
					processEnd("导出成功！");
				}else{
					processError("导出失败！");
				}
			}else{
				processError("暂无数据！");
			}
		},null,null);
	}
	function pagebtns(){
		document.onkeydown = function(e){ 
			$("input").blur();
		    var ev = document.all ? window.event : e;
		    if(ev.keyCode==13) {
		    	getGradeAttence();
		    }
		};
		$("#attence-search-btn").click(function(){
			now = 0;
//			initLeave(1,pageSize);
			getGradeAttence();
		});
		$(".students-choice",parent.document).unbind("click");
		$(".students-choice",parent.document).click(function(){
			$(".students-choice",parent.document).removeClass("classes-choice-select");
			$(this).addClass("students-choice-select");
			now = 0;
			getGradeAttence();
//			initLeave(1,pageSize);
			
		});	
		$("#leadingOut").click(function(){
			var content = "";
			var startTime = $("#start-time").val();
			var endTime = $("#end-time").val();
			if(startTime == "" || endTime == ""){
				content += "--  ";
			}else{
				content += startTime+"  -  "+endTime;
			}
			deleteBox("您选择的时间段是：<span class='span-class'>"+content+"</span>，</br>确定要导出吗？",function(){
				leadingOutAttences();
			});
		});
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();